<?php

namespace app\shopapi\controller;


use app\admin\model\shop\ShopGoodsCategory;
use app\common\controller\Shopapi;
use app\shopapi\model\ShopGoods;


/**
 * 团购接口
 */
class Goods extends Shopapi
{
    protected $noNeedLogin = ['',''];
    protected $noNeedRight = '*';

    //套餐类型
    public function category()
    {
        $lists = ShopGoodsCategory::order('weigh desc')->field('category_id,category_name')->select();
        $this->success('',$lists);
    }


    //添加团购
    public function create()
    {
        $params = input();
        try {

            $model = new \app\shopapi\model\ShopGoods();
            $params['user_id']=$this->shopauth->id;
            $params['shop_id']=$this->shopauth->shop_id;
            $res = $model->add($params);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }


    //添加团购草稿
    public function draft()
    {
        $params = input();
        try {

            $model = new \app\shopapi\model\ShopGoods();
            $params['user_id']=$this->shopauth->id;
            $params['shop_id']=$this->shopauth->shop_id;
            $res = $model->draft($params);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }

    //团购列表
    public function lists()
    {
        $params = input();
        try {

            $model = new \app\shopapi\model\ShopGoods();

            $params['shop_id']=$this->shopauth->shop_id;
            $res = $model->lists($params);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }

    //删除
    public function del()
    {
        $model = new \app\shopapi\model\ShopGoods();
        $goods_id = input('goods_id',0);

        $res = $model->del($goods_id);
        if ($res){
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    //详情
    public function detail()
    {
        $goods_id = input('goods_id',0);
        $detail = ShopGoods::where(['goods_id'=>$goods_id])->find();
        $this->success('',$detail);
    }

    //编辑
    public function edit()
    {
        $params = input();
        try {

            $model = new \app\shopapi\model\ShopGoods();
            //$params['user_id']=$this->shopauth->id;
            $res = $model->edit($params);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }

    //上架
    public function onGoods()
    {
        $goods_id = input('goods_id',0);

        try {
            $model = new \app\shopapi\model\ShopGoods();

            $res = $model->updateGoodsState($goods_id,1);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('上架成功',$res);
    }

    //下架
    public function offGoods()
    {
        $goods_id = input('goods_id',0);

        try {
            $model = new \app\shopapi\model\ShopGoods();

            $res = $model->updateGoodsState($goods_id,2);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('操作成功',$res);
    }
}